home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / pgm / hipstopg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-10  |  4.5 KB  |  180 lines

  1. /* hipstopgm.c - read a HIPS file and produce a portable graymap
  2. **
  3. ** Copyright (C) 1989 by Jef Poskanzer.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "pgm.h"
  14.  
  15. struct HIPS_Header {
  16.     char* orig_name;    /* An indication of the originator of this sequence. */
  17.     char* seq_name;    /* The sequence name. */
  18.     int num_frame;    /* The number of frames in this sequence. */
  19.     char* orig_date;    /* The date the sequence was originated. */
  20.     int rows;        /* The number of rows in each image, the height. */
  21.     int cols;        /* The number of columns in each image, the width. */
  22.     int bits_per_pixel;    /* The number of significant bits per pixel. */
  23.     int bit_packing;    /* Nonzero if the bits were packed such as to
  24.                eliminate any unused bits resulting from a
  25.                bits_per_pixel value which was not an even
  26.                multiple of eight. */
  27.     int pixel_format;    /* An indication of the format of each pixel. */
  28.     char* seq_history;    /* A description of the sequence of transformations
  29.                leading up to the current image. */
  30.     char* seq_desc;    /* A free form description of the contents of the
  31.                sequence. */
  32.     };
  33. #define HIPS_PFBYTE 0
  34. #define HIPS_PFSHORT 1
  35. #define HIPS_PFINT 2
  36. #define HIPS_PFFLOAT 3
  37. #define HIPS_PFCOMPLEX 4
  38.  
  39. static void read_hips_header ARGS(( FILE* fd, struct HIPS_Header* hP ));
  40. static void read_line ARGS(( FILE* fd, char* buf, int size ));
  41.  
  42. void
  43. main( argc, argv )
  44. int argc;
  45. char* argv[];
  46.     {
  47.     FILE* ifp;
  48.     gray* grayrow;
  49.     register gray* gP;
  50.     int argn, row;
  51.     register int col;
  52.     int maxval;
  53.     int rows, cols;
  54.     struct HIPS_Header h;
  55.  
  56.     pgm_init( &argc, argv );
  57.  
  58.     argn = 1;
  59.  
  60.     if ( argn < argc )
  61.     {
  62.     ifp = pm_openr( argv[argn] );
  63.     argn++;
  64.     }
  65.     else
  66.     ifp = stdin;
  67.  
  68.     if ( argn != argc )
  69.     pm_usage( "[hipsfile]" );
  70.  
  71.     read_hips_header( ifp, &h );
  72.  
  73.     cols = h.cols;
  74.     rows = h.rows * h.num_frame;
  75.  
  76.     switch ( h.pixel_format )
  77.     {
  78.     case HIPS_PFBYTE:
  79.     if ( h.bits_per_pixel != 8 )
  80.         pm_error(
  81.         "can't handle unusual bits_per_pixel %d", h.bits_per_pixel );
  82.     if ( h.bit_packing != 0 )
  83.         pm_error( "can't handle bit_packing" );
  84.     maxval = 255;
  85.     break;
  86.  
  87.     default:
  88.     pm_error( "unknown pixel format %d", h.pixel_format );
  89.     }
  90.     if ( maxval > PGM_MAXMAXVAL )
  91.     pm_error(
  92.       "bits_per_pixel is too large - try reconfiguring with PGM_BIGGRAYS" );
  93.  
  94.     pgm_writepgminit( stdout, cols, rows, (gray) maxval, 0 );
  95.     grayrow = pgm_allocrow( cols );
  96.     for ( row = 0; row < rows; row++)
  97.     {
  98.     for ( col = 0, gP = grayrow; col < cols; col++, gP++ )
  99.         {
  100.         int ich;
  101.  
  102.         switch ( h.pixel_format )
  103.         {
  104.         case HIPS_PFBYTE:
  105.         ich = getc( ifp );
  106.         if ( ich == EOF )
  107.             pm_error( "EOF / read error" );
  108.         *gP = (gray) ich;
  109.         break;
  110.  
  111.         default:
  112.         pm_error( "can't happen" );
  113.         }
  114.         }
  115.     pgm_writepgmrow( stdout, grayrow, cols, (gray) maxval, 0 );
  116.     }
  117.     pm_close( ifp );
  118.     pm_close( stdout );
  119.  
  120.     exit( 0 );
  121.     }
  122.  
  123. static void
  124. read_hips_header( fd, hP )
  125. FILE* fd;
  126. struct HIPS_Header* hP;
  127.     {
  128.     char buf[5000];
  129.  
  130.     /* Read and toss orig_name. */
  131.     read_line( fd, buf, 5000 );
  132.  
  133.     /* Read and toss seq_name. */
  134.     read_line( fd, buf, 5000 );
  135.  
  136.     /* Read num_frame. */
  137.     read_line( fd, buf, 5000 );
  138.     hP->num_frame = atoi( buf );
  139.  
  140.     /* Read and toss orig_date. */
  141.     read_line( fd, buf, 5000 );
  142.  
  143.     /* Read rows. */
  144.     read_line( fd, buf, 5000 );
  145.     hP->rows = atoi( buf );
  146.  
  147.     /* Read cols. */
  148.     read_line( fd, buf, 5000 );
  149.     hP->cols = atoi( buf );
  150.  
  151.     /* Read bits_per_pixel. */
  152.     read_line( fd, buf, 5000 );
  153.     hP->bits_per_pixel = atoi( buf );
  154.  
  155.     /* Read bit_packing. */
  156.     read_line( fd, buf, 5000 );
  157.     hP->bit_packing = atoi( buf );
  158.  
  159.     /* Read pixel_format. */
  160.     read_line( fd, buf, 5000 );
  161.     hP->pixel_format = atoi( buf );
  162.  
  163.     /* Now read and toss lines until we get one with just a period. */
  164.     do
  165.     {
  166.     read_line( fd, buf, 5000 );
  167.     }
  168.     while ( strcmp( buf, ".\n" ) != 0 );
  169.     }
  170.  
  171. static void
  172. read_line( fd, buf, size )
  173. FILE* fd;
  174. char* buf;
  175. int size;
  176.     {
  177.     if ( fgets( buf, size, fd ) == NULL )
  178.     pm_error( "error reading header" );
  179.     }
  180.